/*
 * @Author: silence
 * @Date: 2022-04-27 11:51:00
 * @LastEditors: silence
 * @LastEditTime: 2022-04-27 15:31:26
 * @Description: 自定义AppBar
 */

import 'package:flutter/material.dart';

import '../utils/color/ColorUtils.dart';

class MyAppBar extends StatefulWidget implements PreferredSizeWidget {
  /// 导航条高度
  final double navBarHeight;

  /// AppBar背景颜色，默认白色
  final Color? backgroundColor;

  /// 左边按钮
  final Widget? leadingWidget;

  /// 中间title
  final Widget? titleWidget;

  /// 右边按钮
  final Widget? trailingWidget;

  /// 底部分割线
  final bool? bottomline;

  /// 导航分割线，默认不显示
  final bool? underlineHide;

  const MyAppBar(
      {Key? key,
      this.navBarHeight = 44,
      this.backgroundColor = Colors.white,
      this.leadingWidget,
      this.titleWidget,
      this.trailingWidget,
      this.underlineHide = true,
      this.bottomline})
      : super(key: key);

  @override
  State<MyAppBar> createState() => _MyAppBarState();

  @override
  Size get preferredSize => new Size.fromHeight(navBarHeight);
}

class _MyAppBarState extends State<MyAppBar> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.backgroundColor,
      child: SafeArea(
          top: true,
          child: Container(
            height: widget.navBarHeight,
            decoration: (widget.underlineHide == true)
                ? null
                : const UnderlineTabIndicator(
                    borderSide:
                        BorderSide(width: 1.0, color: Color(0xFFeeeeee))),
            child: Stack(
              alignment: Alignment.center,
              children: [
                Positioned(
                    left: 0,
                    child: Padding(
                        padding: const EdgeInsets.only(left: 5),
                        child: widget.leadingWidget)),
                Positioned(
                    left: 50, child: Container(child: widget.titleWidget,width: 250,)),
                Positioned(
                    right: 0,
                    child: Container(
                        padding: const EdgeInsets.only(right: 5),
                        child: widget.trailingWidget)),
                Positioned(
                    bottom: 0,
                    child: widget.bottomline == true
                        ? Container(
                            height: 1,
                            width: MediaQuery.of(context).size.width,
                            color: rgba(0, 0, 0, 0.05))
                        : Container()),
              ],
            ),
          )),
    );
  }
}
